#include "palette.h"

static bool upKeyPressed = false;
static bool downKeyPressed = false;

Palette::Palette( std::vector<sf::Image> &tiles, float left, float top, unsigned int width, unsigned height ): width( width ), height( height ), left( left ), top( top )
{
	this->index = 0;
	this->numOfTiles = (int)tiles.size();
	this->endIndex = (int)ceil( (float)( this->numOfTiles / this->width ) );
	this->upInput = sf::Key::Up;
	this->downInput = sf::Key::Down;
	this->tiles = &tiles;
}

Palette::Palette( Palette &palette )
{
	this->index = palette.index;
	this->width = palette.width;
	this->height = palette.height;
	this->left = palette.left;
	this->top = palette.top;
	this->upInput = palette.upInput;
	this->downInput = palette.downInput;
	this->endIndex = palette.endIndex;
}
Palette::~Palette()
{
}
void Palette::setPosition(float left, float top)
{
	this->left = left;
	this->top = top;
}

void Palette::setWidth( unsigned int width )
{
	this->width = width;
}

void Palette::setHeight( unsigned int height )
{
	this->height = height;
}

void Palette::input( const sf::Input &input )
{
	if( input.IsKeyDown( this->upInput ) && upKeyPressed != true)
	{
		this->index--;
		if( this->index < 0 )
		{
			this->index = 0;
		}
		upKeyPressed = true;
	}
	if( input.IsKeyDown( this->downInput ) && downKeyPressed != true )
	{
		this->index++;
		if( this->index > this->endIndex )
		{
			this->index = this->endIndex;
		}
		downKeyPressed = true;
	}
	if( !input.IsKeyDown( this->downInput ) )
	{
		downKeyPressed = false;
	}
	if( input.IsKeyDown( this->upInput ) )
	{
		upKeyPressed = false;
	}
}

void Palette::draw(sf::RenderWindow &app)
{
	float tmpWidth = left;
	float tmpHeight = top;

	int k = 0;
	//////////////////////////////////////////
	//this needs to be put in the constructor
	sf::Sprite currentSprite;
	sf::Image blankImage;
	blankImage.LoadFromFile( "blankTile.png" );
	//////////////////////////////////////////
	for( unsigned int i = 0; i < this->height; i++ )
	{
		for( unsigned int j = 0; j < this->width; j++ )
		{
			if( k < this->numOfTiles )
			{
				currentSprite.SetImage( this->tiles->at( k ) );
				currentSprite.SetPosition( tmpWidth, tmpHeight );
				app.Draw( currentSprite );
				tmpWidth += TILE_SIZE;
				k++;
			}
			else
			{
				currentSprite.SetImage( blankImage );
				currentSprite.SetPosition( (float)tmpWidth, (float)tmpHeight );
				app.Draw( currentSprite );
				tmpWidth += TILE_SIZE;
			}
		}
		tmpHeight += TILE_SIZE;
		tmpWidth = left;
	}
	/// Draw boarder around the tiles
	tmpWidth = left;
	tmpHeight = top;
	sf::Image boarderImage;
	boarderImage.LoadFromFile( "borderLeft.png" );
	currentSprite.SetImage( boarderImage );
	sf::Sprite topSprite;
	topSprite.SetImage( boarderImage );
	topSprite.Rotate( 90 );
	for( unsigned int i = 0; i < this->height; i++ )
	{
		for( unsigned int j = 0; j < this->width; j++ )
		{
			if( j == this->width - 1 )
			{
				currentSprite.SetPosition( tmpWidth + TILE_SIZE, tmpHeight );
				currentSprite.Rotate( 180 );
				app.Draw( currentSprite );
			}	
			if( j == 0 )
			{
				currentSprite.SetPosition( tmpWidth - 1, tmpHeight );
				app.Draw( currentSprite );
				currentSprite.Rotate( 180 );
				
			}
			if( i == 0 )
			{
				topSprite.SetPosition( tmpWidth, tmpHeight );
				app.Draw( topSprite );
				if( j == this->width - 1 )
				{
					topSprite.Rotate( 180 );
				}
			}
			if( i == this->height - 1 )
			{
				topSprite.SetPosition( tmpWidth + TILE_SIZE + 1, tmpHeight + TILE_SIZE );
				app.Draw( topSprite );
			}
			tmpWidth += TILE_SIZE;
		}
		tmpHeight += TILE_SIZE;
		tmpWidth = left;
	}
}